package com.l.util;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

/**
 * Created by gyy on 2016/6/18.
 */
public class ExcelUtil {
    public final static List<List<Object>> read(String path) throws IOException, InvalidFormatException {
        List<List<Object>> rows = new ArrayList<List<Object>>();
        List<Object> cells;

        Workbook wb = WorkbookFactory.create(new File(path));
        Sheet sheet = wb.getSheetAt(0);

        for(Iterator<Row> rowIterator = sheet.rowIterator(); rowIterator.hasNext();) {
            Row row = rowIterator.next();
            // 最大列数
            cells = new ArrayList<Object>(row.getLastCellNum());
            for(Iterator<Cell> cellIterator = row.cellIterator(); cellIterator.hasNext();) {
                cells.add(cellIterator.next());
            }
            rows.add(cells);
        }

        return rows;
    }
}
